// Package _8_I_翻转单词顺序 main
// @Title _8_I_翻转单词顺序
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-01-13 20:11
// @Create  2022-01-13 20:11
package main

import (
	"fmt"
	"strings"
)

func main() {
	//fmt.Println(reverseWords("the sky is blue"))
	fmt.Println(reverseWords("  hello world!  "))
	//fmt.Println(reverseWords("a good   example"))
}

//
//  reverseWords
//  @Description: 翻转字符串的顺序，但是不翻转单词内部的顺序
//  @param s： 输入的字符串
//  @return string：翻转后的字符串
//
func reverseWords(s string) string {
	var res string
	var i = len(s) - 1
	var j = i
	for i >= 0 {
		for i >= 0 && s[i] == ' ' {
			i--
		}
		j = i
		for i >= 0 && s[i] != ' ' {
			i--
		}
		res += s[i+1:j+1] + " "
	}
	return strings.TrimRight(res, " ")
}
